﻿<cfsilent>

	<cfquery name="queryDepart" datasource="U_IMP_SYNC">
		SELECT 
			depid, depname, deptype, depshort
		FROM 
			imp_department
	</cfquery>

	<cfset syncOpt = event.getArg("SynOpt", "new") />

	<cfset dataAdded = 0 />
	<cfset dataUpdated = 0 />
    
    <cfset rs_departmentType = queryNew("classification")/>
	
</cfsilent>


<cfswitch expression="#syncOpt#">
	<cfcase value="new">
	
		<p>同步模式: 仅追加新纪录</p>
		
		<cftry>
			<cftransaction>
				<cfloop query="queryDepart">
					<!--- 循环中心数据库记录, 尝试追加新纪录 --->
				
                    <cfset sql = "SELECT 1 
									FROM t_institute a 
									WHERE a.institute_id = :insId "/>
                    
                    <cfset queryObj = new Query(datasource = application.dnsMaster)/>
                    <cfset queryObj.addParam(name="insId", value=queryDepart.depid, cfsqltype="cf_sql_varchar")/>
                    
                    <cfset rs_department = queryObj.execute(sql=sql).getResult()/>
					<cfif rs_department.recordCount EQ 0>
						<cfif rs_departmentType.recordCount GT 0 and rs_departmentType.classification eq queryDepart.deptype>
							<!--- 无需更新 departmentType 组件 --->
							<cfelse>
							
                                <cfset sql = "SELECT a.classification 
												FROM t_institute_type a 
												WHERE a.classification = :insId "/>
                    
								<cfset queryObj = new Query(datasource = application.dnsMaster)/>
                                <cfset queryObj.addParam(name="insId", value=queryDepart.deptype, cfsqltype="cf_sql_char")/>
                                
                                <cfset rs_departmentType = queryObj.execute(sql=sql).getResult()/>
						</cfif>
						<!--- 记录不存在 需要追加 --->
					
                        <cfset sql = "INSERT INTO t_institute (
											   institute_id,
											   institute_name,
											   institute_short,
											   classification
										) VALUES (
											   :InstituteID,
											   :InstituteName,
											   :InstituteShortName,
											   :Classification 
										) "/> 
                                        
                        <cfset queryObj = new Query(datasource = application.dnsMaster)/>
                        <cfset queryObj.addParam(name="InstituteID", value=queryDepart.depid, cfsqltype="cf_sql_varchar")/>
                        <cfset queryObj.addParam(name="InstituteName", value=queryDepart.depname, cfsqltype="cf_sql_varchar")/>
                        <cfset queryObj.addParam(name="InstituteShortName", value=queryDepart.depshort, cfsqltype="cf_sql_varchar")/>
                        <cfset queryObj.addParam(name="Classification", value=queryDepart.deptype, cfsqltype="cf_sql_char")/>
                        <cfset queryObj.execute(sql=sql)/>
						<!--- 计数器累加 --->
						<cfset dataAdded++ />
						<!--- 写入数据库 --->
					</cfif>
				</cfloop>
			</cftransaction>

			<cfcatch type="any">
				<cfset hasError = true />
				<p class="warn">记录处理异常: <cfoutput>#cfcatch.Detail#</cfoutput></p>
				<p class="warn">回滚数据</p>
			</cfcatch>

		</cftry>
		
	</cfcase>
	<cfcase value="update">

		<p>同步模式: 同步更新所有数据</p>
		<p class="warn">警告: 如果现有数据某些字段发生变更后没有及时转发至数据中心, 这些变更将丢失</p>
		
		<cftry>
		
			<cftransaction>
				<cfloop query="queryDepart">
					<!--- 循环中心数据库记录, 尝试追加新纪录 --->
				
                    <cfset sql = "SELECT a.institute_id 
									FROM t_institute a 
									WHERE a.institute_id = :insId "/>
                    
                    <cfset queryObj = new Query(datasource = application.dnsMaster)/>
                    <cfset queryObj.addParam(name="insId", value=queryDepart.depid, cfsqltype="cf_sql_varchar")/>
                    
                    <cfset rs_department = queryObj.execute(sql=sql).getResult()/>
					<cfif rs_departmentType.recordCount GT 0 and rs_departmentType.classification eq queryDepart.deptype>
							<!--- 无需更新 departmentType 组件 --->
						<cfelse>
						
                            <cfset sql = "SELECT a.classification 
												FROM t_institute_type a 
												WHERE a.classification = :insId "/>
                    
							<cfset queryObj = new Query(datasource = application.dnsMaster)/>
                            <cfset queryObj.addParam(name="insId", value=queryDepart.deptype, cfsqltype="cf_sql_char")/>
                            
                            <cfset rs_departmentType = queryObj.execute(sql=sql).getResult()/>
					</cfif>
					<cfif not isDefined("department")>
							<!--- 记录不存在 需要追加 --->
						
							<!--- 计数器累加 --->
							<cfset dataAdded++ />
							<!--- 写入数据库 --->
						
                            <cfset sql = "INSERT INTO t_institute (
											   institute_id,
											   institute_name,
											   institute_short,
											   classification
										) VALUES (
											   :InstituteID,
											   :InstituteName,
											   :InstituteShortName,
											   :Classification 
										) "/> 
                                        
							<cfset queryObj = new Query(datasource = application.dnsMaster)/>
                            <cfset queryObj.addParam(name="InstituteID", value=queryDepart.depid, cfsqltype="cf_sql_varchar")/>
                            <cfset queryObj.addParam(name="InstituteName", value=queryDepart.depname, cfsqltype="cf_sql_varchar")/>
                            <cfset queryObj.addParam(name="InstituteShortName", value=queryDepart.depshort, cfsqltype="cf_sql_varchar")/>
                            <cfset queryObj.addParam(name="Classification", value=queryDepart.deptype, cfsqltype="cf_sql_char")/>
                            <cfset queryObj.execute(sql=sql)/>
						<cfelse>
							<!--- 记录已存在 根据中心数据进行更新 --->
							
							<!--- 计数器累加 --->
							<cfset dataUpdated++ />
							<!--- 写入数据库 --->
							
                            <cfset sql = "UPDATE  t_institute 
										   SET   
											   institute_name = :InstituteName,
											   institute_short = :InstituteShortName,
											   classification = :Classification 
										  WHERE institute_id = :InstituteID "/> 
                                        
							<cfset queryObj = new Query(datasource = application.dnsMaster)/>
                            <cfset queryObj.addParam(name="InstituteID", value=rs_department.institute_id, cfsqltype="cf_sql_varchar")/>
                            <cfset queryObj.addParam(name="InstituteName", value=queryDepart.depname, cfsqltype="cf_sql_varchar")/>
                            <cfset queryObj.addParam(name="InstituteShortName", value=queryDepart.depshort, cfsqltype="cf_sql_varchar")/>
                            <cfset queryObj.addParam(name="Classification", value=queryDepart.deptype, cfsqltype="cf_sql_char")/>
                            <cfset queryObj.execute(sql=sql)/>
					</cfif>
				</cfloop>
			</cftransaction>
		
			<cfcatch type="any">
				<cfset hasError = true />
				<p class="warn">记录处理异常: <cfoutput>#cfcatch.Detail#</cfoutput></p>
				<p class="warn">回滚数据</p>
			</cfcatch>
		
		</cftry>
		
	</cfcase>
</cfswitch>

<cfif not isDefined("hasError")>
	<p class="notice">
		追加数据 <cfoutput>#dataAdded#</cfoutput> 条, 更新数据 <cfoutput>#dataUpdated#</cfoutput> 条,
		获取 <cfoutput>#queryDepart.recordCount#</cfoutput> 条单位数据记录
	</p>
</cfif>

